/*
 * @Author: boyyang
 * @Date: 2022-01-02 22:15:32
 * @LastEditTime: 2022-02-08 17:53:34
 * @LastEditors: boyyang
 * @Description: 登录注册
 * @FilePath: \boyyang\src\cloudbase\login.ts
 */

import cloudbase from '@cloudbase/js-sdk'
import { auth } from "./init"


/**
 * @description: 邮箱 登录
 * @param {string} account
 * @param {string} password
 * @return {登录状态信息}
 */
const signInByEmail = (account: string, password: string | number): Promise<cloudbase.auth.ILoginState> => {
    return auth.signInWithEmailAndPassword(account, String(password))
}

/**
 * @description: 用户名 登录
 * @param {string} userName
 * @param {string} password
 * @return {登录状态信息}
 */
const singInByUserName = (userName: string, password: string | number): Promise<cloudbase.auth.ILoginState> => {
    return auth.signInWithUsernameAndPassword(userName, String(password))
}

/**
 * @description: 邮箱 注册
 * @param {string} acount
 * @param {string} password
 * @return {Promise<void>}
 */
const signUpByEmail = (acount: string, password: string | number): Promise<void> => {
    return auth.signUpWithEmailAndPassword(acount, String(password))
}

/**
 * @description: 登出
 * @param {*} 
 * @return {Promise<void>}
 */
const signOut = (): Promise<void> => {
    return auth.signOut()
}

/**
 * @description: 用户登录信息
 * @param {*} 
 * @return {Promise<null | 用户登录信息>}
 */
const loginState = (): Promise<cloudbase.auth.ILoginState | null> => {
    return auth.getLoginState()
}


/**
 * @description: 
 * @param {*} cloudbase
 * @return {loudbase.auth.IUser | null}
 */
const userMes = (): cloudbase.auth.IUser | null => {
    return auth.currentUser
}

/**
 * @description: 更新用户信息
 * @param {any} option
 * @return {*}
 */
const userMesUpdate = (option: any) => {
    return auth.currentUser?.update({
        ...option
    })
}

export {
    signUpByEmail,
    singInByUserName,
    signInByEmail,
    signOut,
    loginState,
    userMes,
    userMesUpdate
}
